package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class IncreaseIdUtils {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final long START = 1735689600;
    public static final int COUNT = 32;

    public long getId() {
        // 获取时间戳
        LocalDateTime now = LocalDateTime.now();
        long timestamp = now.toEpochSecond(ZoneOffset.UTC) - START;

        // 获取当前日期的id
        String format = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd:"));
        Long currentId = stringRedisTemplate.opsForValue().increment("idMake:" + format);

        return (timestamp << COUNT) | currentId;
    }
}
